#include "charactertemplate.h"


void CCharacterXMLParser::startElement(void *ctx, const char *name, const char **atts)
{
	_lastelement = (char*)name;
}

void CCharacterXMLParser::endElement(void *ctx, const char *name)
{
	_lastelement = "";
}

void CCharacterXMLParser::textHandler(void *ctx, const char *ch, int len)
{
	string value = std::string((char*)ch,0,len);

	if(_lastelement == "id")
	{
		_lastTemplate = new TEMPLATE_TYPE();
		_lastTemplate->id = atoi(value.c_str());

		CGameTemplate::GetThis()->SetTemplate(_type, _lastTemplate);
	}
	else if(_lastelement == "name")
		_lastTemplate->name = value;
	else if(_lastelement == "kind")
		_lastTemplate->kind = atoi(value.c_str());
	else if(_lastelement == "move_speed")
		_lastTemplate->move_speed = atoi(value.c_str());
	else if(_lastelement == "att_delay")
		_lastTemplate->att_delay = atoi(value.c_str());
	else if(_lastelement == "ani_id")
		_lastTemplate->ani_id = atoi(value.c_str());
	else if(_lastelement == "ai_see_range")
		_lastTemplate->ai_see_range = atoi(value.c_str());

}

bool CCharacterXMLParser::initParsing()
{
	_animFile = "template/character.xml";
	_type = GAMETEMPLATE::CHARACTER;


	CCSAXParser parser;

	if (false == parser.init("UTF-8"))
	{
		return false;
	}
	parser.setDelegator(this);

	const char *xmlPath = CCFileUtils::fullPathFromRelativePath(_animFile);
	parser.parse(xmlPath);
	return true;
}
